package com.qing.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.eduservice.entity.pojo.EduCourse;
import com.qing.eduservice.entity.vo.ChapterVo;
import com.qing.eduservice.entity.vo.CourseInfoVo;
import com.qing.eduservice.entity.vo.CourseQuery;
import com.qing.eduservice.entity.vo.CourseWebVo;
import com.qing.eduservice.service.EduChapterService;
import com.qing.eduservice.service.EduCourseService;
import com.qing.servicebase.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Yjx
 * @since 2022-09-08
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    private final EduCourseService eduCourseService;

    private final EduChapterService eduChapterService;

    public EduCourseController(EduCourseService eduCourseService, EduChapterService eduChapterService) {
        this.eduCourseService = eduCourseService;
        this.eduChapterService = eduChapterService;
    }

    //根据课程id查询课程信息
    @GetMapping("/getDto/{courseId}")
    public CourseInfoVo getCourseInfoDto(@PathVariable String courseId) {
        ResponseResult result = eduCourseService.getCourseInfoFormById(courseId);
        CourseInfoVo courseInfoVo = (CourseInfoVo)result.getData();
        CourseInfoVo courseInfo = new CourseInfoVo();
        BeanUtils.copyProperties(courseInfoVo,courseInfo);
        return courseInfo;
    }

    @GetMapping(value = "/get/{courseId}")
    public ResponseResult getterById(@PathVariable String courseId){
//查询课程信息和讲师信息
        ResponseResult result1 = eduCourseService.selectInfoWebById(courseId);
        Object data1 = result1.getData();
//查询当前课程的章节信息
        ResponseResult resule2 = eduChapterService.nestedList(courseId);
        Object data2 = resule2.getData();
        Map<String, Object> map = new HashMap<>();
        map.put("course",data1);
        map.put("chapterVoList",data2);
        return ResponseResult.ofSuccess(map);
    }

    @PostMapping("/addCourse")
    public ResponseResult saveCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        return eduCourseService.saveCourseInfo(courseInfoVo);
    }

    @GetMapping("/courseInfo/{id}")
    public ResponseResult getById(@PathVariable String id){
        return eduCourseService.getCourseInfoFormById(id);
    }

    @PutMapping("/update/{id}")
    public ResponseResult updateCourseInfoById(@RequestBody CourseInfoVo courseInfoVo, @PathVariable String id){
        return eduCourseService.updateCourseInfoById(courseInfoVo);
    }

    @GetMapping("/search/{id}")
    public ResponseResult getCoursePublishVoById(@PathVariable String id){
        return eduCourseService.getCoursePublishVoById(id);
    }

    @PutMapping("/publish/{id}")
    public ResponseResult publishCourseById(@PathVariable String id){
        return eduCourseService.publishCourseById(id);
    }

    @GetMapping("/page/{size}/{pageSize}")
    public ResponseResult pageQuery(@PathVariable("size") int size,
                                    @PathVariable("pageSize") int pageSize,
                                    CourseQuery courseQuery){
        Page<EduCourse> pageParam = new Page<>(size, pageSize);
        return eduCourseService.pageQuery(pageParam,courseQuery);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult removeById(@PathVariable("id") String id){
        return eduCourseService.removeByCourseId(id);
    }
}

